package br.com.management.mentor.model.entities.auth;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.security.core.GrantedAuthority;

@Entity
public class Permission implements GrantedAuthority {

	private static final long serialVersionUID = 5025663527685654942L;

	@Id
	@GeneratedValue( strategy = GenerationType.IDENTITY )
	private Long id;

	@NotBlank
	@Length( max = 80 )
	@Column( nullable = false , unique = true , length = 80 , updatable = false )
	private String name;

	@NotNull
	@Column( nullable = false , updatable = false )
	@Temporal( TemporalType.TIMESTAMP )
	private Date creationDate;

	public String getAuthority() {
		return name;
	}

	public Long getId() {
		return id;
	}

	public void setId( Long id ) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName( String name ) {
		this.name = name;
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate( Date creationDate ) {
		this.creationDate = creationDate;
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append( name ).append( creationDate ).toHashCode();
	}

	@Override
	public boolean equals( Object obj ) {
		if ( !( obj instanceof Permission ) )
			return false;
		Permission other = (Permission) obj;
		return new EqualsBuilder().append( name , other.name ).isEquals();
	}
}